package com.bootdo.system.controller;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.Model;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.bootdo.system.domain.DataDictionaryDO;
import com.bootdo.system.service.DataDictionaryService;
import com.bootdo.common.domain.DictDO;
import com.bootdo.common.utils.PageUtils;
import com.bootdo.common.utils.Query;
import com.bootdo.common.utils.R;

/**
 * 数据字典表
 * 
 * @author yuanqiang
 * @email 1005724903@qq.com
 * @date 2018-08-13 10:24:50
 */
 
@Controller
@RequestMapping("/system/dataDictionary")
public class DataDictionaryController {
	@Autowired
	private DataDictionaryService dataDictionaryService;
	
	@GetMapping()
	//@RequiresPermissions("system:dataDictionary:dataDictionary")
	String DataDictionary(){
	    return "system/dataDictionary/dataDictionary";
	}
	
	@ResponseBody
	@GetMapping("/list")
	//@RequiresPermissions("system:dataDictionary:dataDictionary")
	public PageUtils list(@RequestParam Map<String, Object> params){
		//查询列表数据
        Query query = new Query(params);
		List<DataDictionaryDO> dataDictionaryList = dataDictionaryService.list(query);
		int total = dataDictionaryService.count(query);
		PageUtils pageUtils = new PageUtils(dataDictionaryList, total);
		return pageUtils;
	}
	
	@GetMapping("/add")
	//@RequiresPermissions("system:dataDictionary:add")
	String add(){
	    return "system/dataDictionary/add";
	}

	@GetMapping("/edit/{id}")
	//@RequiresPermissions("system:dataDictionary:edit")
	String edit(@PathVariable("id") Long id,Model model){
		DataDictionaryDO dataDictionary = dataDictionaryService.get(id);
		model.addAttribute("dataDictionary", dataDictionary);
	    return "system/dataDictionary/edit";
	}
	
	/**
	 * 保存
	 */
	@ResponseBody
	@PostMapping("/save")
	//@RequiresPermissions("system:dataDictionary:add")
	public R save( DataDictionaryDO dataDictionary){
		dataDictionary.setCreateTime(new Date());
		dataDictionary.setUpdateTime(new Date());
		if(dataDictionaryService.save(dataDictionary)>0){
			return R.ok();
		}
		return R.error();
	}
	/**
	 * 修改
	 */
	@ResponseBody
	@RequestMapping("/update")
	//@RequiresPermissions("system:dataDictionary:edit")
	public R update( DataDictionaryDO dataDictionary){
		dataDictionary.setUpdateTime(new Date());
		dataDictionaryService.update(dataDictionary);
		return R.ok();
	}
	
	/**
	 * 删除
	 */
	@PostMapping( "/remove")
	@ResponseBody
	//@RequiresPermissions("system:dataDictionary:remove")
	public R remove( Long id){
		if(dataDictionaryService.remove(id)>0){
		return R.ok();
		}
		return R.error();
	}
	
	/**
	 * 删除
	 */
	@PostMapping( "/batchRemove")
	@ResponseBody
	//@RequiresPermissions("system:dataDictionary:batchRemove")
	public R remove(@RequestParam("ids[]") Long[] ids){
		dataDictionaryService.batchRemove(ids);
		return R.ok();
	}
	
	
	@ResponseBody
	@GetMapping("/list/{type}")
	public List<DataDictionaryDO> listByType(@PathVariable("type") String type) {
		// 查询列表数据
		Map<String, Object> map = new HashMap<>(16);
		map.put("code", type);
		map.put("recStatus", "A");
		//Query query = new Query(map);
		List<DataDictionaryDO> dataDictionaryList = dataDictionaryService.list(map);
		//List<DictDO> dictList = dataDictionaryService.list(map);
		return dataDictionaryList;
	}
	
}
